home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr05 / ew120.zip / FILES1.ZIP / COUNTER.PAS < prev    next >
Pascal/Delphi Source File  |  1993-05-02  |  3KB  |  118 lines

  1. {************************************************}
  2. {                                                }
  3. { E! for Windows                                 }
  4. { (c) - Patrick Philippot - 1992,1993            }
  5. {                                                }
  6. { Sample Extension DLL - version 1.1             }
  7. {                                                }
  8. { This DLL implements a simple word counter.     }
  9. { When executed it will count the number of      }
  10. { words and characters contained in the current  }
  11. { Editor and display the result in a message box.}
  12. {                                                }
  13. {************************************************}
  14.  
  15. (*
  16. To use this DLL simply load it from the user menu or add its name to the
  17. list of autoloaded Extension DLLs using the Autoload dialog box from
  18. the User Menu of EW. That's all.
  19.  
  20. This Extension DLL uses no hook. Since it attaches itself to the User Menu,
  21. you don't need to run it from the User|Execute Extension Menu. A "Word
  22. Counter" option automatically appears in the User Menu. You may also assign
  23. this extension to a keystroke. See the documentation for a description of
  24. how to assign DLL execution to a keystroke.
  25. *)
  26.  
  27. {$I compdir.inc}
  28. {$C MOVEABLE PRELOAD DISCARDABLE}
  29.  
  30. library Counter;
  31.  
  32. uses WinProcs, WinTypes, EWApiImp, Strings;
  33.  
  34. const
  35.   Title : PChar = 'Word Counter';
  36.  
  37. var
  38.   SaveExit : Pointer;
  39.   EntryId  : longint;
  40.  
  41.  
  42. function Words(src : PChar) : longint;
  43.  
  44. var
  45.   Count : longint;
  46.   i,
  47.   len   : word;
  48.  
  49. const
  50.   Delimiters : set of char =
  51.     ['.', ' ', ',', ';', ':', '\', '/', '(', ')', '{', '}', '[', ']', '-'];
  52.  
  53. begin
  54.   len := StrLen(src);
  55.   if len = 0 then
  56.     Count := 0
  57.   else begin
  58.     Count := 1;
  59.     i := 0;
  60.     while (i < len) and (src[i] in Delimiters) do
  61.       Inc(i);
  62.     repeat
  63.       while (i < len) and not (src[i] in Delimiters) do
  64.         Inc(i);
  65.       while (i < len) and (src[i] in Delimiters) do
  66.         Inc(i);
  67.       if i < len then
  68.         Inc(Count);
  69.     until i >= len;
  70.   end;
  71.   Words := Count;
  72. end;
  73.  
  74. function EWExecute(RoutineId : word) : integer; export;
  75.  
  76. var
  77.   index        : integer;
  78.   CharCount,
  79.   WordCount    : longint;
  80.   WordCountStr : array [0..7] of char;
  81.   CharCountStr : array [0..10] of char;
  82.   Message      : array [0..255] of char;
  83.   P            : PChar;
  84.  
  85. begin
  86.   WordCount := 0;
  87.   CharCount := 0;
  88.   for index := 0 to Pred(EWGetLineCount) do begin
  89.     P := EWGetLineAt(index);
  90.     Inc(WordCount, Words(P));
  91.     Inc(CharCount, StrLen(P));
  92.   end;
  93.   StrCopy(Message, EWGetFileName(EWGetCurrentEditor));
  94.   StrCat(Message, ' contains ');
  95.   Str(WordCount, WordCountStr);
  96.   Str(CharCount, CharCountStr);
  97.   StrCat(StrCat(StrCat(StrCat(Message, WordCountStr), ' words and '), CharCountStr), ' characters.');
  98.   MessageBox(EWGetWindowHandle, Message, Title, mb_IconInformation or mb_Ok);
  99.   EWExecute := 0;
  100. end;
  101.  
  102. procedure LibExit; far;
  103. begin
  104.  {-Remove menu item from User Menu before unloading}
  105.   EWRemoveMenuEntry(EntryId);
  106.   ExitProc := SaveExit;
  107. end;
  108.  
  109. exports
  110.   EWExecute     index 1;
  111.  
  112. begin
  113.   SaveExit := ExitProc;
  114.   ExitProc := @LibExit;
  115.  {-Extension attaches itself to the user Menu}
  116.   EntryId := EWAddMenuEntry('counter', Title, 0, EWMNU_Extension, 0);
  117. end.
  118.